﻿#region License
/* ---------------------------------------------------------------- *
 *
 * This file is part of the Xcoordination Application Space
 * ("XcoAppSpace") http://www.xcoordination.com
 *
 * Copyright (C) 2009 Xcoordination http://www.xcoordination.com
 *
 * XcoAppSpace is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General
 * Public License as published by the Free Software Foundation.
 *
 * XcoAppSpace is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/
 * or write to the Free Software Foundation, Inc., 51 Franklin
 * Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ---------------------------------------------------------------- */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Ccr.Core;
using XcoAppSpaces.Contracts.Service;
using XcoAppSpaces.Core;

namespace XcoAppSpaces.Contracts.Worker
{
	/// <summary>
	/// Presents information about a worker in the space.
	/// </summary>
	public class XcoWorkerInfo
	{
		/// <summary>
		/// The name of the worker.
		/// </summary>
		public string Name { get; private set; }

		/// <summary>
		/// True if the worker is published (reachable by other appspace instances).
		/// </summary>
		public bool IsPublished { get; private set; }

		/// <summary>
		/// True if the worker is not local, but actually hosted by another appspace instance.
		/// </summary>
		public bool IsRemote { get; private set; }

		/// <summary>
		/// The remote address of the worker, or null if the worker is local.
		/// </summary>
		public string RemoteAddress { get; private set; }

		/// <summary>
		/// Information about the transport service that the worker uses, or null
		/// if the worker is local.
		/// </summary>
		public string TransportServiceName { get; private set; }

		/// <summary>
		/// Identifier of the worker, built from the worker's type and name
		/// </summary>
		public string Identifier { get; private set; }

		/// <summary>
		/// The worker instance.
		/// </summary>
		public IPort Worker { get; private set; }

		/// <summary>
		/// The contract type of the worker.
		/// </summary>
		public Type ContractType { get; private set; }

		/// <summary>
		/// List of attributes of the worker, that could be interesting to services of the space.
		/// </summary>
		public IEnumerable<XcoServiceAttributeInfo> ServiceAttributes { get; private set; }

		/// <summary>
		/// List of attributes of the processors of this worker.
		/// </summary>
		public Dictionary<Type, XcoProcessorAttribute> ProcessorAttributes { get; private set; }

		/// <summary>
		/// The number of connections to the worker (= how many times the "ConnectWorker" method
		/// was called to connect to this worker).
		/// </summary>
		public int NumberOfConnections { get; set; }

		/// <summary>
		/// Creates a new XcoWorkerInfo.
		/// </summary>
		/// <param name="instance">The worker instance.</param>
		/// <param name="contractType">The contract type of the worker.</param>
		/// <param name="name">The name of the worker.</param>
		/// <param name="publishMode">The publish mode of the worker, specifies if the worker is visible to remote spaces.</param>
		/// <param name="serviceAttributes">List of attributes of the worker, that could be interesting to services of the space.</param>
		/// <param name="processorAttributes">List of attributes of the processors of this worker.</param>
		/// <param name="isRemote">True if the worker is not local, but actually hosted by another appspace instance.</param>
		/// <param name="remoteAddress">The remote address of the worker, or null if the worker is local.</param>
		/// <param name="transportServiceName">Information about the transport service that the worker uses, or null if the worker is local.</param>
		/// <param name="identifier">Identifier of the worker, built out of the worker's type and name.</param>
		public XcoWorkerInfo(IPort instance, Type contractType, string name, PublishMode publishMode, List<XcoServiceAttributeInfo> serviceAttributes, Dictionary<Type, XcoProcessorAttribute> processorAttributes, bool isRemote, string remoteAddress, string transportServiceName, string identifier)
		{
			this.Worker = instance;
			this.ContractType = contractType;
			this.Name = name;
			this.IsPublished = publishMode == PublishMode.Published;
			this.ServiceAttributes = serviceAttributes ?? new List<XcoServiceAttributeInfo>();
			this.ProcessorAttributes = processorAttributes ?? new Dictionary<Type, XcoProcessorAttribute>();
			this.IsRemote = isRemote;
			this.RemoteAddress = remoteAddress;
			this.TransportServiceName = transportServiceName;
			this.NumberOfConnections = 0;
			this.Identifier = identifier;
		}
	}
}
